/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actionforms;

import edu.ipfw.cs590.cheat.UserRights;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.LabelValueBean;

/**
 *
 * @author safar
 */
public class RegisterUserForm extends org.apache.struts.action.ActionForm {

    /**
     * User name.
     */
    private String userName;
    /**
     * First name.
     */
    private String firstName;
    /**
     * Last name.
     */
    private String lastName;
    /**
     * Phone.
     */
    private String phone;
    /**
     * URL.
     */
    private String url;
    /**
     * E-mail.
     */
    private String email;
    /**
     * verified email.
     */
    private String emailVerified;
    /**
     * Password.
     */
    private String password;
    /**
     * Verified password.
     */
    private String passwordVerified;
    /**
     * Access type.
     */
    private UserRights accessType;
    /**
     * Affiliation.
     */
    private String affiliation;

    /**
     * Constructor.
     */
    public RegisterUserForm() {
        super();
        accessType = UserRights.STUDENT;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the emailVerified
     */
    public String getEmailVerified() {
        return emailVerified;
    }

    /**
     * @param emailVerified the emailVerified to set
     */
    public void setEmailVerified(String emailVerified) {
        this.emailVerified = emailVerified;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the passwordVerified
     */
    public String getPasswordVerified() {
        return passwordVerified;
    }

    /**
     * @param passwordVerified the passwordVerified to set
     */
    public void setPasswordVerified(String passwordVerified) {
        this.passwordVerified = passwordVerified;
    }

    /**
     * @return the accessType
     */
    public String getAccessType() {
        return accessType.toString();
    }

    /**
     * @param accessType the accessType to set
     */
    public void setAccessType(String accessType) {
        this.accessType = UserRights.valueOf(accessType);
    }

    /**
     * @return the affiliation
     */
    public String getAffiliation() {
        return affiliation;
    }

    /**
     * @param affiliation the affiliation to set
     */
    public void setAffiliation(String affiliation) {
        this.affiliation = affiliation;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * Get the possible access types.
     * @return List of access types.
     */
    public List getQueryAccessType() {

        // build the duration list based on the duration value
        List list = new ArrayList();

        for (UserRights v : UserRights.values()) {
            if (!v.equals(UserRights.ADMINISTRATOR)) {
                LabelValueBean lvb = new LabelValueBean(v.name(), v.name());
                list.add(lvb);
            }
        }
        return list;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        //Test all required properties
        propertyRequired(errors, "User Name", userName, 5, 15);
        propertyRequired(errors, "First Name", firstName);
        propertyRequired(errors, "Last Name", lastName);
        propertyRequired(errors, "Phone Number", phone);
        propertyRequired(errors, "URL", url);
        propertyRequired(errors, "Email Address", email);
        propertyRequired(errors, "Password", password, 6, 20);
        propertyRequired(errors, "Affilliation", affiliation);

        //Special email match check
        if (!email.equals(emailVerified)) {
            errors.add("Email Mismatch", new ActionMessage("errors.mismatch", "Email Address", "Verified Email Address"));
        }

        //Special password match check
        if (!password.equals(passwordVerified)) {
            errors.add("Passowrd Mismatch", new ActionMessage("errors.mismatch", "Password", "Verified Password"));
        }

        //Special access type check
        if (accessType == null || accessType.equals(UserRights.ADMINISTRATOR)) {
            errors.add("Access Type", new ActionMessage("errors.invalid", "Access Type"));
        }

        return errors;
    }

    /**
     * Property required.
     * @param errors
     * @param propName
     * @param propValue
     */
    public void propertyRequired(ActionErrors errors, String propName, String propValue) {
        if (propValue == null || propValue.length() < 1) {
            errors.add(propName, new ActionMessage("errors.required", propName));
        }
    }

    /**
     * Property required.
     * @param errors
     * @param propName
     * @param propValue
     * @param lower
     * @param upper
     */
    public void propertyRequired(ActionErrors errors, String propName, String propValue, int lower, int upper) {
        propertyRequired(errors, propName, propValue);
        if (propValue != null && propValue.length() < lower) {
            errors.add(propName, new ActionMessage("errors.minlength", propName, lower));
        } else if (propValue != null && propValue.length() > upper) {
            errors.add(propName, new ActionMessage("errors.maxlength", propName, upper));
        }
    }
}
